<?php

namespace app\wanqing\controller;

use app\wanqing\model\WanqingFollowModel;
use app\wanqing\model\WanqingVirtualAddressModel;
use app\wanqing\model\WanqingVirtualAddressRenewModel;
use cmf\controller\AdminBaseController;

class VirtualAddressController extends AdminBaseController
{
    /**
     * 虚拟地址列表
     * @adminMenu(
     *     'name'   => '虚拟地址列表',
     *     'parent' => 'business/LicensePlate/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 100,
     *     'icon'   => '',
     *     'remark' => '虚拟地址列表',
     *     'param'  => ''
     * )
     */
    function index()
    {
        $virtualAddressModel = new WanqingVirtualAddressModel();

        $where = [];
        $uid = cmf_get_current_admin_id();
        if ($uid != 1) {
            $users = implode(',', getUsersForRole($uid));
            $where['virtualAddress.uid'] = ['in', $users];
        }

        if (input('keyword')) {
            $keyword = input('keyword');
            $where['virtualAddress.id|virtualAddress.address_name|virtualAddress.home_no|virtualAddress.from_to|virtualAddress.company|virtualAddress.remark|user.user_nickname'] = ['like', "%$keyword%"];
        }

        if (input('cus_type')) {
            $cus_type = input('cus_type');
            $where['virtualAddress.cus_type'] = $cus_type - 1;
        }

        $lists = $virtualAddressModel
            ->alias('virtualAddress')
            ->join('__USER__ user', 'virtualAddress.uid = user.id')
            ->field('virtualAddress.*,user.user_nickname')
            ->where($where)
            ->order('id desc')
            ->paginate();

        $where_r = input();
        $lists->appends($where_r);
        $page = $lists->render();

        $this->assign('where_r', $where_r);
        $this->assign('lists', $lists);
        $this->assign('page', $page);
        return $this->fetch();
    }

    /**
     * 添加
     * @adminMenu(
     *     'name'   => '添加',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加',
     *     'param'  => ''
     * )
     */
    function add()
    {
        if ($this->request->isPost()) {
            $arrData = $this->request->post();

            $virtualAddressModel = new WanqingVirtualAddressModel();
            $arrData['uid'] = cmf_get_current_admin_id();
            $result = $virtualAddressModel->allowField(true)->validate(true)->save($arrData);
            if ($result) {
                $this->success('添加成功', url('index'));
            } else {
                $this->error($virtualAddressModel->getError());
            }
        }
        return $this->fetch();
    }

    /**
     * 编辑
     * @adminMenu(
     *     'name'   => '编辑',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑',
     *     'param'  => ''
     * )
     */
    function edit()
    {
        if ($this->request->isPost()) {
            $arrData = $this->request->post();
            if (empty($arrData['id'])) {
                $this->error('参数错误');
            }

            $virtualAddressModel = new WanqingVirtualAddressModel();
            $result = $virtualAddressModel->allowField(true)->validate(true)->save($arrData, [
                'id' => $arrData['id']
            ]);
            if ($result) {
                $this->success('保存成功', url('index'));
            } else {
                $this->error($virtualAddressModel->getError());
            }
        }
        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            $this->error('参数错误');
        }
        $virtualAddressModel = new WanqingVirtualAddressModel();
        $result = $virtualAddressModel
            ->alias('virtualAddress')
            ->join('__USER__ user', 'virtualAddress.uid = user.id')
            ->field('virtualAddress.*,user.user_nickname')
            ->where([
                'virtualAddress.id' => $id
            ])
            ->find();

        $this->assign('result', $result);
        return $this->fetch();
    }

    /**
     * 续费
     * @adminMenu(
     *     'name'   => '续费',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '续费',
     *     'param'  => ''
     * )
     */
    function renew()
    {
        $vid = $this->request->param('vid', 0, 'intval');
        if (!$vid) {
            $this->error('参数错误');
        }
        $virtualAddressRenewModel = new WanqingVirtualAddressRenewModel();
        $lists = $virtualAddressRenewModel->where([
            'vid' => $vid
        ])->order('pay_time desc')->paginate();

        $page = $lists->render();

        $this->assign('lists', $lists);
        $this->assign('page', $page);
        $this->assign('vid', $vid);
        return $this->fetch();
    }

    /**
     * 添加
     * @adminMenu(
     *     'name'   => '添加',
     *     'parent' => 'renew',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加',
     *     'param'  => ''
     * )
     */
    function renew_add(){
        $vid = $this->request->param('vid', 0, 'intval');
        if (!$vid) {
            $this->error('参数错误');
        }
        if($this->request->isPost()) {
            $arrData = $this->request->post();
            $data['start_time'] = $arrData['start_time'];
            $data['end_time'] = $arrData['end_time'];

            $virtualAddressModel = new WanqingVirtualAddressModel();
            $virtualAddressModel->save($data,['id'=>$vid]);

            $virtualAddressRenewModel = new WanqingVirtualAddressRenewModel();

            $data['price'] = $arrData['price'];
            $data['pay_time'] = strtotime($arrData['pay_time']);
            $data['vid'] = $vid;
            $data['uid'] = cmf_get_current_admin_id();
            $virtualAddressRenewModel->save($data);
            $this->success('添加成功',url('renew',['vid'=>$vid]));
        }
        $this->assign('vid', $vid);
        return $this->fetch();
    }

    /**
     * 跟进
     * @adminMenu(
     *     'name'   => '跟进',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '跟进',
     *     'param'  => ''
     * )
     */
    function follow()
    {
        $pid = $this->request->param('pid', 0, 'intval');
        if(!$pid) {
            $this->error('非法访问');
        }
        $wanqingFollowModel = new WanqingFollowModel();
        $where = ['type' => 1, 'pid' => $pid];
        $lists = $wanqingFollowModel
            ->alias('follow')
            ->join('__USER__ user', 'follow.uid = user.id')
            ->field('follow.*,user.user_nickname as uname')
            ->where($where)
            ->order(['id desc'])
            ->paginate(10);
        $page = $lists->render();

        $this->assign('lists', $lists);
        $this->assign('page', $page);
        $this->assign('pid', $pid);
        return $this->fetch();
    }

    /**
     * 添加跟进
     * @adminMenu(
     *     'name'   => '添加跟进',
     *     'parent' => 'follow',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加跟进',
     *     'param'  => ''
     * )
     */
    function add_follow()
    {
        $pid = $this->request->param('pid', 0, 'intval');
        if(!$pid) {
            $this->error('访问错误！');
        }
        if($this->request->isPost()) {
            $wanqingFollowModel = new WanqingFollowModel();
            $arrData = $this->request->post();

            $time = time();
            $arrData['create_time'] = $time;
            $arrData['type'] = 1;
            $arrData['uid'] = cmf_get_current_admin_id();
            $arrData['next_time'] = strtotime($arrData['next_time']);
            if ($arrData['next_time'] < $time) {
                $arrData['next_time'] = 0;
            }
            if(!$arrData['pid']) {
                $this->error('非法访问');
            }

            $wanqingFollowModel->allowField(true)->insertGetId($arrData);

            $this->success(lang("ADD_SUCCESS"), url("follow", ['pid' => $arrData['pid']]));
        }
        $this->assign('pid', $pid);
        return $this->fetch();
    }

    /**
     * 编辑跟进
     * @adminMenu(
     *     'name'   => '编辑跟进',
     *     'parent' => 'follow',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑跟进',
     *     'param'  => ''
     * )
     */
    function edit_follow()
    {
        $id = $this->request->param('id', 0, 'intval');
        $wanqingFollowModel = new WanqingFollowModel();
        $result = $wanqingFollowModel->where(['id' => $id])->find();
        if(!$result) {
            $this->error('访问错误！');
        }

        if($this->request->isPost()) {
            $arrData = $this->request->post();

            $time = time();
            $arrData['next_time'] = strtotime($arrData['next_time']);
            if ($arrData['next_time'] < $time) {
                $arrData['next_time'] = 0;
            }

            $wanqingFollowModel->allowField(true)->where(["id" => $id])->update($arrData);

            $this->success(lang("EDIT_SUCCESS"), url("follow", ['pid' => $arrData['pid']]));
        }

        $this->assign('result', $result);
        return $this->fetch();
    }
}